import io from '@hyoga/uni-socket.io'
import { serverUrl } from '../const'
import store from '../store'

export default class WebSock {

	private static ws = null
	public meDisconnect = false

	timer = null

	public async connect(url, path = "/socket.io", token) {

		if (WebSock.ws != null) {
			this.meDisconnect = true
			WebSock.ws.off()
			WebSock.ws.disconnect()
			if (this.timer != null) {
				clearInterval(this.timer)
			}
		}

		WebSock.ws = io(url, {
			path,
			query: {},
			transports: ['websocket', 'polling'],
			timeout: 5000,
			reconnectionAttempts: 10,
			auth: { token, platform: 'app' }
		})

		// this.timer = setInterval(() => {
		// 	if (WebSock.ws) {
		// 		console.log('timer inter val')
		// 		WebSock.ws.emit('ping', {}, res => {
		// 			console.log('ping resp', res)
		// 			uni.showToast({title: res.data || ''})
		// 		})
		// 	}
		// }, 10000)

		WebSock.ws.on('connect', () => {
			console.log('websock connection')
			this.meDisconnect = false
			uni.$emit('socketConnect')
		})

		WebSock.ws.on('disconnect', () => {
			console.log('disconnect from websocket')
			if (this.meDisconnect) return
			uni.showLoading({
				title: '网络连接断开，正在重连',
				mask: true
			})
		})

		// 刷新架构
		WebSock.ws.on('REFRESH_CONTACT', () => {
			console.log('刷新架构')
			store.contachtList = null
			uni.$emit('refresh_contact')
		})

		// 新任务
		WebSock.ws.on('TASK-NEW', () => {
			console.log('新任务')
			uni.$emit('TASK-NEW')
		})

		// 任务变动
		WebSock.ws.on('TASK-CHANGE', () => {
			console.log('任务变动')
			uni.$emit('TASK-CHANGE')
		})
		// 任务撤销
		WebSock.ws.on('TASK-REVERT', id => {
			console.log('任务撤销')
			uni.$emit('TASK-REVERT', id)
		})

		// 新通知
		WebSock.ws.on('NOTIFICATION-NEW', () => {
			console.log('新通知')
			uni.$emit('NOTIFICATION-NEW')
		})

		// 通知变动
		WebSock.ws.on('NOTIFICATION-CHANGE', () => {
			console.log('通知变动')
			uni.$emit('NOTIFICATION-CHANGE')
		})

		// 重新登录
		WebSock.ws.on('RE-LOGIN', () => {
			console.log('re login')
			this.disconnect()
			uni.showToast({ title: '登录凭证过期，请重新登录' })
			store.token = ''
			uni.setStorageSync('token', '')
			uni.redirectTo({ url: '/pages/login/index' })
		})

		// 账号在其他地方登录
		WebSock.ws.on('DUPLICATE-LOGIN', () => {
			console.log('duplicat login')
			this.disconnect()
			uni.showToast({ title: '账号在别处登录' })
			store.token = ''
			uni.setStorageSync('token', '')
			uni.redirectTo({ url: '/pages/login/index' })
		})

		// 重连成功
		WebSock.ws.io.on('reconnect', () => {
			this.meDisconnect = false
			console.log('reconnect to websocket success')
			uni.hideLoading()
		})

		// 正在重连
		WebSock.ws.io.on('reconnect_attempt', () => {
			console.log('attempt reconnect to websocket')
		})

		// 重连失败
		WebSock.ws.io.on('reconnect_failed', () => {
			console.log('reconnect to websocket failed')
			uni.hideLoading()
			uni.redirectTo({ url: '/pages/login/index' })
		})
	}

	// 断开连接
	public disconnect() {
		if (WebSock.ws == null) return
		this.meDisconnect = true
		WebSock.ws.off()
		WebSock.ws.disconnect()
		WebSock.ws = null
	}

	// 进行请求
	public async performRequest(route, params = {}) {
		console.log('perform request', route, params, store)
		if (!store.token) return uni.redirectTo({ url: '/pages/login/index' })
		if (WebSock.ws == null) {
			websock.connect('wss://' + serverUrl, '/socket-io', store.token)
		}
		return new Promise((resolve, reject) => {
			WebSock.ws.emit(route, params, res => {
				if (res.code == 9999) {
					console.log('need to relogin', res)
					store.token = ''
					uni.setStorageSync('token', '')
					uni.showToast({ title: '请重新登录' })
					return uni.redirectTo({ url: '/pages/login/index' })
				}

				console.log('perform request', route, params, res)
				// if (res.code != 0) return reject(new Error(res.message))
				if (res.code != 0) return uni.showToast({ title: res.message, icon: 'none' })

				resolve(res.data)
			})
		})
	}

	// ping
	public ping() {
		return this.performRequest('ping')
	}

	// 获取信息
	public async getUserInfo() {
		if (store.userInfo) return store.userInfo
		let res = await this.performRequest('userInfo')
		store.userInfo = res
		return res
	}

	// 修改昵称
	public modifyName(params) {
		return this.performRequest('modifyName', params)
	}

	// 获取联系人列表 
	public async getContactList() {
		if (store.contachtList) return store.contachtList
		let res = await this.performRequest('contactList')
		store.contachtList = res
		return res
	}

	// 获取部门列表
	public async getDepartList() {
		if (store.departList) return store.departList
		let res = await this.performRequest('departList')
		store.departList = res
		return res
	}

	// 获取岗位列表
	public async getPosList() {
		if (store.posList) return store.posList
		let res = await this.performRequest('posList')
		store.posList = res
		return res
	}

	// 获取今日打卡详情
	public async getTodayDakaDetail(params) {
		return this.performRequest('todayDakaDetail', params)
	}

	// 打卡
	public async daka(params) {
		return this.performRequest('daka', params)
	}
	// 打卡列表
	public async dakaList(params) {
		return this.performRequest('dakaList', params)
	}
	// 补卡申请
	public async bukaApply(params) {
		return this.performRequest('bukaApply', params)
	}
	public async revertBukaApply(params) {
		return this.performRequest('revertBukaApply', params)
	}
	public async bukaApplyRecord(params) {
		return this.performRequest('bukaApplyRecord', params)
	}
	public async bukaApplyDetail(params) {
		return this.performRequest('bukaApplyDetail', params)
	}

	// 获取考勤设置
	public async getKaoqinSetting(params) {
		return this.performRequest('getKaoqinSetting', params)
	}

	// 更新考勤设置
	public async updateKaoqinSetting(params) {
		return this.performRequest('updateKaoqinSetting', params)
	}

	// 个人考勤统计
	public async personalKaoqinStatis(params) {
		return this.performRequest('personalKaoqinStatis', params)
	}

	// 部门考勤统计
	public async groupKaoqinStatis(params) {
		return this.performRequest('groupKaoqinStatis', params)
	}

	// 请假申请
	public vacationApply(params) {
		return this.performRequest('vacationApply', params)
	}
	public revertVacationApply(params) {
		return this.performRequest('revertVacationApply', params)
	}
	public xiaojia(params) {
		return this.performRequest('xiaojia', params)
	}
	public vacationApplyRecord(params) {
		return this.performRequest('vacationApplyRecord', params)
	}
	public vacationApplyDetail(params) {
		return this.performRequest('vacationApplyDetail', params)
	}

	// 出差申请
	public chuchaiApply(params) {
		return this.performRequest('chuchaiApply', params)
	}
	public revertChuchaiApply(params) {
		return this.performRequest('revertChuchaiApply', params)
	}
	public chuchaiApplyRecord(params) {
		return this.performRequest('chuchaiApplyRecord', params)
	}
	public chuchaiApplyDetail(params) {
		return this.performRequest('chuchaiApplyDetail', params)
	}

	// 加班申请
	public jiabanApply(params) {
		return this.performRequest('jiabanApply', params)
	}
	public revertJiabanApply(params) {
		return this.performRequest('revertJiabanApply', params)
	}
	public jiabanApplyRecord(params) {
		return this.performRequest('jiabanApplyRecord', params)
	}
	public jiabanApplyDetail(params) {
		return this.performRequest('jiabanApplyDetail', params)
	}
	public getUserDakaSetting(params) {
		return this.performRequest('getUserDakaSetting', params)
	}
	public updateUserDakaSetting(params) {
		return this.performRequest('updateUserDakaSetting', params)
	}

	// 获取未完成的任务数量
	public getUnfinishedTaskCnt() {
		return this.performRequest('getUnfinishedTaskCnt')
	}

	// 获取任务列表
	public getTaskList(params) {
		return this.performRequest('getTaskList', params)
	}
	public getTaskDetail(params) {
		return this.performRequest('getTaskDetail', params)
	}
	public getTaskListYiwancheng(params) {
		return this.performRequest('getTaskListYiwancheng', params)
	}
	public getTaskListWeiwancheng(params) {
		return this.performRequest('getTaskListWeiwancheng', params)
	}

	// 同意任务
	public agreeTask(params) {
		return this.performRequest('agreeTask', params)
	}

	// 拒绝任务
	public async rejectTask(params) {
		return this.performRequest('rejectTask', params)
	}

	// 确认任务
	public async confirmTask(params) {
		return this.performRequest('confirmTask', params)
	}

	// 获取未读通知的数量
	public getUnreadNotificationCnt() {
		return this.performRequest('getUnreadNotificationCnt')
	}

	// 获取通知列表
	public getNotificationList(params) {
		return this.performRequest('getNotificationList', params)
	}

	// 阅读通知
	public readNotification(params) {
		return this.performRequest('readNotification', params)
	}

	// 获取通知信息
	public getNotificationInfo(params) {
		return this.performRequest('getNotificationInfo', params)
	}

	// 入职申请
	public entryApply(params) {
		return this.performRequest('entryApply', params)
	}
	public revertEntryApply(params) {
		return this.performRequest('revertEntryApply', params)
	}
	public entryApplyRecord(params) {
		return this.performRequest('entryApplyRecord', params)
	}
	public entryApplyDetail(params) {
		return this.performRequest('entryApplyDetail', params)
	}

	// 转正申请
	public zhuanzhengApply(params) {
		return this.performRequest('zhuanzhengApply', params)
	}
	public revertZhuanzhengApply(params) {
		return this.performRequest('revertZhuanzhengApply', params)
	}
	public zhuanzhengApplyRecord(params) {
		return this.performRequest('zhuanzhengApplyRecord', params)
	}
	public zhuanzhengApplyDetail(params) {
		return this.performRequest('zhuanzhengApplyDetail', params)
	}

	// 离职申请申请
	public departureApply(params) {
		return this.performRequest('departureApply', params)
	}
	public revertDepartureApply(params) {
		return this.performRequest('revertDepartureApply', params)
	}
	public departureApplyRecord(params) {
		return this.performRequest('departureApplyRecord', params)
	}
	public departureApplyDetail(params) {
		return this.performRequest('departureApplyDetail', params)
	}

	// 工资单
	public gongziRecord(params) {
		return this.performRequest('gongziRecord', params)
	}

	// 督办单申请
	public dubandanApply(params) {
		return this.performRequest('dubandanApply', params)
	}
	public revertDubandanApply(params) {
		return this.performRequest('revertDubandanApply', params)
	}
	public dubandanApplyRecord(params) {
		return this.performRequest('dubandanApplyRecord', params)
	}
	public dubandanApplyDetail(params) {
		return this.performRequest('dubandanApplyDetail', params)
	}

	// 会议室申请
	public meetingRoomApply(params) {
		return this.performRequest('meetingRoomApply', params)
	}
	public revertMeetingRoomApply(params) {
		return this.performRequest('revertMeetingRoomApply', params)
	}
	public meetingRoomApplyRecord(params) {
		return this.performRequest('meetingRoomApplyRecord', params)
	}
	public meetingRoomApplyDetail(params) {
		return this.performRequest('meetingRoomApplyDetail', params)
	}

	// 接待申请
	public jiedaiApply(params) {
		return this.performRequest('jiedaiApply', params)
	}
	public revertJiedaiApply(params) {
		return this.performRequest('revertJiedaiApply', params)
	}
	public jiedaiApplyRecord(params) {
		return this.performRequest('jiedaiApplyRecord', params)
	}
	public jiedaiApplyDetail(params) {
		return this.performRequest('jiedaiApplyDetail', params)
	}

	// 加油申请
	public jiayouApply(params) {
		return this.performRequest('jiayouApply', params)
	}
	public revertJiayouApply(params) {
		return this.performRequest('revertJiayouApply', params)
	}
	public jiayouApplyRecord(params) {
		return this.performRequest('jiayouApplyRecord', params)
	}
	public jiayouApplyDetail(params) {
		return this.performRequest('jiayouApplyDetail', params)
	}

	// 用车申请
	public yongcheApply(params) {
		return this.performRequest('yongcheApply', params)
	}
	public revertYongcheApply(params) {
		return this.performRequest('revertYongcheApply', params)
	}
	public yongcheApplyRecord(params) {
		return this.performRequest('yongcheApplyRecord', params)
	}
	public yongcheApplyDetail(params) {
		return this.performRequest('yongcheApplyDetail', params)
	}

	// 用印申请
	public yongyinApply(params) {
		return this.performRequest('yongyinApply', params)
	}
	public revertYongyinApply(params) {
		return this.performRequest('revertYongyinApply', params)
	}
	public yongyinApplyRecord(params) {
		return this.performRequest('yongyinApplyRecord', params)
	}
	public yongyinApplyDetail(params) {
		return this.performRequest('yongyinApplyDetail', params)
	}

	// 发起会议通知
	public newHuiyiTongzhi(params) {
		return this.performRequest('newHuiyiTongzhi', params)
	}
	public huiyiTongzhiRecord(params) {
		return this.performRequest('huiyiTongzhiRecord', params)
	}
	public huiyiTongzhiDetail(params) {
		return this.performRequest('huiyiTongzhiDetail', params)
	}

	// 发起工作通知
	public newGongzuoTongzhi(params) {
		return this.performRequest('newGongzuoTongzhi', params)
	}
	public gongzuoTongzhiRecord(params) {
		return this.performRequest('gongzuoTongzhiRecord', params)
	}
	public gongzuoTongzhiDetail(params) {
		return this.performRequest('gongzuoTongzhiDetail', params)
	}

	// 采购申请
	public caigouApply(params) {
		return this.performRequest('caigouApply', params)
	}
	public revertCaigouApply(params) {
		return this.performRequest('revertCaigouApply', params)
	}
	public caigouApplyRecord(params) {
		return this.performRequest('caigouApplyRecord', params)
	}
	public caigouApplyDetail(params) {
		return this.performRequest('caigouApplyDetail', params)
	}

	// 物资领用
	public wuzilingyong(params) {
		return this.performRequest('wuzilingyong', params)
	}
	public revertWuzilingyong(params) {
		return this.performRequest('revertWuzilingyong', params)
	}
	public wuzilingyongRecord(params) {
		return this.performRequest('wuzilingyongRecord', params)
	}
	public wuzilingyongDetail(params) {
		return this.performRequest('wuzilingyongDetail', params)
	}

	// 合同审批
	public hetongShenpi(params) {
		return this.performRequest('hetongShenpi', params)
	}
	public revertHetongShenpi(params) {
		return this.performRequest('revertHetongShenpi', params)
	}
	public hetongShenpiRecord(params) {
		return this.performRequest('hetongShenpiRecord', params)
	}
	public hetongShenpiDetail(params) {
		return this.performRequest('hetongShenpiDetail', params)
	}

	// 招标审核
	public zhaobiaoShenhe(params) {
		return this.performRequest('zhaobiaoShenhe', params)
	}
	public revertZhaobiaoShenhe(params) {
		return this.performRequest('revertZhaobiaoShenhe', params)
	}
	public zhaobiaoShenheRecord(params) {
		return this.performRequest('zhaobiaoShenheRecord', params)
	}
	public zhaobiaoShenheDetail(params) {
		return this.performRequest('zhaobiaoShenheDetail', params)
	}

	// 传阅文件
	public chuanyueWenjian(params) {
		return this.performRequest('chuanyueWenjian', params)
	}
	public revertChuanyueWenjian(params) {
		return this.performRequest('revertChuanyueWenjian', params)
	}
	public chuanyueWenjianRecord(params) {
		return this.performRequest('chuanyueWenjianRecord', params)
	}
	public chuanyueWenjianDetail(params) {
		return this.performRequest('chuanyueWenjianDetail', params)
	}

	// 公文流转
	public gongwenLiuzhuan(params) {
		return this.performRequest('gongwenLiuzhuan', params)
	}
	public revertGongwenLiuzhuan(params) {
		return this.performRequest('revertGongwenLiuzhuan', params)
	}
	public finishGongwenLiuzhuan(params) {
		return this.performRequest('finishGongwenLiuzhuan', params)
	}
	public gongwenLiuzhuanRecord(params) {
		return this.performRequest('gongwenLiuzhuanRecord', params)
	}
	public gongwenLiuzhuanDetail(params) {
		return this.performRequest('gongwenLiuzhuanDetail', params)
	}

	// 内部发文
	public neibuFawen(params) {
		return this.performRequest('neibuFawen', params)
	}
	public revertneibuFawen(params) {
		return this.performRequest('revertneibuFawen', params)
	}
	public neibuFawenRecord(params) {
		return this.performRequest('neibuFawenRecord', params)
	}
	public neibuFawenDetail(params) {
		return this.performRequest('neibuFawenDetail', params)
	}

	// 新建事项
	public xinjianFawen(params) {
		return this.performRequest('xinjianFawen', params)
	}
	public revertxinjianFawen(params) {
		return this.performRequest('revertxinjianFawen', params)
	}
	public xinjianFawenRecord(params) {
		return this.performRequest('xinjianFawenRecord', params)
	}
	public xinjianFawenDetail(params) {
		return this.performRequest('xinjianFawenDetail', params)
	}
	// 宣传报道
	public xuanchuanbaodao(params) {
		return this.performRequest('xuanchuanbaodao', params)
	}
	public reverxuanchuanbaodao(params) {
		return this.performRequest('reverxuanchuanbaodao', params)
	}
	public xuanchuanbaodaoRecord(params) {
		return this.performRequest('xuanchuanbaodaoRecord', params)
	}
	public xuanchuanbaodaoDetail(params) {
		return this.performRequest('xuanchuanbaodaoDetail', params)
	}
	// 工作餐
	public gongzuocan(params) {
		return this.performRequest('gongzuocan', params)
	}
	public revergongzuocan(params) {
		return this.performRequest('revergongzuocan', params)
	}
	public gongzuocanRecord(params) {
		return this.performRequest('gongzuocanRecord', params)
	}
	public gongzuocanDetail(params) {
		return this.performRequest('gongzuocanDetail', params)
	}
	// 公文信息
	public getGongwenInfo(params) {
		return this.performRequest('getGongwenInfo', params)
	}

	// 收藏公文
	public shoucangGongwen(params) {
		return this.performRequest('shoucangGongwen', params)
	}

	public cancelShoucang(params) {
		return this.performRequest('cancelShoucang', params)
	}

	// 获取公文收藏列表
	public getGongwenShoucangList(params) {
		return this.performRequest('getGongwenShoucangList', params)
	}

	// 获取公文历史列表
	public getGongwenHistoryList(params) {
		return this.performRequest('getGongwenHistoryList', params)
	}
	public getGongwenHistoryListType(params) {
		return this.performRequest('getGongwenHistoryListType', params)
	}

	// 根据uid获取用户信息
	public getUserInfoByUid(params) {
		return this.performRequest('getUserInfoByUid', params)
	}

	// 根据uid获取岗位信息
	public getDpInfoByUid(params) {
		return this.performRequest('getDpInfoByUid', params)
	}

	public getXiuxiDayListApp(params) {
		return this.performRequest('getXiuxiDayListApp', params)
	}
	public addPaiban(params) {
		return this.performRequest('addPaiban', params)
	}
	public paibanDetail(params) {
		return this.performRequest('paibanDetail', params)
	}

	public paibanStatis(params) {
		return this.performRequest('paibanStatis', params)
	}

	public paibanMonthDetail(params) {
		return this.performRequest('paibanMonthDetail', params)
	}

	public chepaiList(params) {
		return this.performRequest('chepaiList', params)
	}

	public yinzhangList(params) {
		return this.performRequest('yinzhangList', params)
	}

	public huiyishiList(params) {
		return this.performRequest('huiyishiList', params)
	}

	public async qingjiaList(params) {
		if (store.qingjiaList) return store.qingjiaList
		let res = await this.performRequest('qingjiaList', params)
		store.qingjiaList = res
		return res
	}

	public getLingdao(params) {
		return this.performRequest('getLingdao', params)
	}

	public getDepartmentLingdao(params) {
		return this.performRequest('getDepartmentLingdao', params)
	}

	public getAuditors(params) {
		return this.performRequest('getAuditors', params)
	}

	public getUserPermissionsApp(params) {
		return this.performRequest('getUserPermissionsApp', params)
	}

	public getUserGroupList(params) {
		return this.performRequest('getUserGroupList', params)
	}

	public getShangbanDetail(params) {
		return this.performRequest('getShangbanDetail', params)
	}

	public exportGroupStatis(params) {
		return this.performRequest('exportGroupStatis', params)
	}

	public cuiban(params) {
		return this.performRequest('cuiban', params)
	}

	// 文件标签 列表
	public fileTagList() {
		return this.performRequest('fileTagList')
	}
}

export let websock = new (WebSock)
